//
//  SubscribeRecommandCell.m
//  suRead
//
//  Created by lidi on 2018/12/9.
//  Copyright © 2018 bao. All rights reserved.
//

#import "SubscribeRecommandCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "UIControl+Blocks.h"

@interface SubscribeRecommandCell()
@property(nonatomic,strong)UIView *serviceContent;
@property(nonatomic,copy)ClickHandle handle;
@property(nonatomic,copy)MoreHandle moreHandle;
@end

@implementation SubscribeRecommandCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *banner = [UIView new];
        [self.contentView addSubview:banner];
        [banner mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(banner.superview);
            make.height.equalTo(@44.0);
        }];
        
        UILabel *labelTag = [UILabel new];
        labelTag.text = @"推荐关注";
        labelTag.textColor = [UIColor darkGrayColor];
        labelTag.font = [UIFont fontWithName:@"FZBIAOYSJW--GB1-0" size:17.0f];
        [banner addSubview:labelTag];
        [labelTag mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labelTag.superview).offset(10.0f);
            make.centerY.equalTo(labelTag.superview);
        }];
        
        UIButton *buttonMore = [UIButton buttonWithType:UIButtonTypeCustom];
        [buttonMore setTitle:@"更多 >" forState:UIControlStateNormal];
        [buttonMore setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        buttonMore.titleLabel.font = [UIFont fontWithName:@"FZBIAOYSJW--GB1-0" size:13.0f];
        __weak typeof(self) weakSelf = self;
        [buttonMore addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
            if (weakSelf.moreHandle) {
                weakSelf.moreHandle();
            }
        }];
        [banner addSubview:buttonMore];
        [buttonMore mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(buttonMore.superview);
            make.top.bottom.equalTo(buttonMore.superview);
            make.width.equalTo(@60.0f);
        }];
        
        UIScrollView *serviceScrollView = [UIScrollView new];
        serviceScrollView.showsHorizontalScrollIndicator = NO;
        [self.contentView addSubview:serviceScrollView];
        [serviceScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(serviceScrollView.superview);
            make.top.equalTo(banner.mas_bottom);
        }];
        
        self.serviceContent = [UIView new];
        [serviceScrollView addSubview:self.serviceContent];
        [self.serviceContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.serviceContent.superview);
            make.height.equalTo(serviceScrollView.mas_height);
        }];
    }
    return self;
}

- (void)refresh:(NSArray *)service_item handle:(ClickHandle)handle moreHandle:(MoreHandle)moreHandle {
    
    self.handle = handle;
    self.moreHandle = moreHandle;
    
    for (UIView *subview in self.serviceContent.subviews) {
        [subview removeFromSuperview];
    }
    
    UIView *preView;
    
    for (NSInteger index = 0; index < [service_item count]; index++) {
        UIView *itemView = [UIView new];
        itemView.layer.cornerRadius = 3.0f;
        itemView.layer.masksToBounds = YES;
        itemView.userInteractionEnabled = YES;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTap:)];
        itemView.tag = index;
        [itemView addGestureRecognizer:ges];
        itemView.backgroundColor = [UIColor redColor];
        [self.serviceContent addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(itemView.superview);
            make.width.equalTo(@120.0f);
            if (!preView) {
                make.left.equalTo(itemView.superview).offset(10.0f);
            } else {
                make.left.equalTo(preView.mas_right).offset(5.0f);
            }
            
            if (index == [service_item count] - 1) {
                make.right.equalTo(itemView.superview).offset(-10.0f);
            }
        }];
        
        preView = itemView;
        
        //内容
        NSDictionary *item = [service_item objectAtIndex:index];
        
        UIImageView *backroundImageView = [UIImageView new];
        [backroundImageView sd_setImageWithURL:[NSURL URLWithString:[item objectForKey:@"service_img"]]];
        [itemView addSubview:backroundImageView];
        [backroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(backroundImageView.superview);
        }];
        
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        effectView.alpha = .9f;
        [itemView addSubview:effectView];
        [effectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(effectView.superview);
            make.height.equalTo(@50.0f);
        }];
        
        UIView *bottomView = [UIView new];
        bottomView.backgroundColor = [UIColor whiteColor];
        [itemView addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(bottomView.superview);
            make.top.equalTo(effectView.mas_bottom);
        }];
        
        UILabel *labelDes = [UILabel new];
        labelDes.text = [item objectForKey:@"service_desc"];
        labelDes.textColor = [UIColor lightGrayColor];
        labelDes.font = [UIFont fontWithName:@"FZBIAOYSJW--GB1-0" size:11.0f];
        labelDes.textAlignment = NSTextAlignmentCenter;
        [bottomView addSubview:labelDes];
        [labelDes mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(labelDes.superview).offset(-10.0f);
            make.left.equalTo(labelDes.superview).offset(10.0f);
            make.right.equalTo(labelDes.superview).offset(-10.0f);
        }];
        
        UILabel *labelName = [UILabel new];
        labelName.text = [item objectForKey:@"service_name"];
        labelName.textColor = [UIColor darkGrayColor];
        labelName.font = [UIFont fontWithName:@"FZBIAOYSJW--GB1-0" size:15.0f];
        labelName.textAlignment = NSTextAlignmentCenter;
        [bottomView addSubview:labelName];
        [labelName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(labelDes.mas_top).offset(-10.0f);
            make.left.equalTo(labelName.superview).offset(10.0f);
            make.right.equalTo(labelName.superview).offset(-10.0f);
        }];
        
        UIImageView *iconImageView = [UIImageView new];
        iconImageView.layer.cornerRadius = 25.0f;
        iconImageView.layer.masksToBounds = YES;
        [iconImageView sd_setImageWithURL:[NSURL URLWithString:[item objectForKey:@"service_img"]]];
        [itemView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(50.0f, 50.0f));
            make.top.equalTo(iconImageView.superview).offset(20.0f);
            make.centerX.equalTo(iconImageView.superview);
        }];
        
    }
}

- (void)itemTap:(UIGestureRecognizer *)ges {
    if (self.handle) {
        self.handle(ges.view.tag);
    }
}

@end
